Skip to content

Conversation

ckganesan
Copy link
Contributor

@ckganesan ckganesan commented Feb 27, 2024

This PR is raised to add support for optional string and array access. #540 #547 #573

"g",
},
{
`'string'?.[7]`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not exactly correct behavior I'm looking for.

In case array?.[7] operator ?. checks what array itself defined and not nil.

So the code

array?.[7]

will panic in there is no 7 element index.

What we can do is to alter [7] behavior with operator ?. after it.

array[7]?.field

In this case we can alter OpFetch and do not panic if 7 index does not exist.

This mean we, unfortunately, does not have a syntax to alter [7] behavior if it is the last postfix op.

But we can alter the whole "chain" in presence of ?? operator!

array[7].field ?? 'default'

@antonmedv antonmedv closed this Mar 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants